//
//  XQCityManager.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/11.
//

import UIKit
import HandyJSON

class XQCityManager: NSObject {

    static let shared = XQCityManager()
    
    private override init() {
        let cacheData : String = XQCache.getCityData()
        
        if !cacheData.isEmpty {
            if let data = XQCityListDataModel.deserialize(from: cacheData) {
                cityModel = data
            }
        }
        
    }
    
    var cityModel : XQCityListDataModel?
    
    public func refreshData() {
        XQNetworkManager.shared.getRequest(url: "/figures/respondents") {[weak self] response, error in
            if (response?.object == 0) {
                guard let srongify = self else {return}
                if let model = XQCityListDataModel.deserialize(from: response?.procedure) {
                    srongify.cityModel = model
                    
                    if let json = srongify.jsonString(from: model) {
                        XQCache.saveCityData(jsString: json)
                    }
                }
            }
        }
    }
}

extension XQCityManager {
    
    public func getProvinceList() -> [String] {
        guard let cityM = cityModel else {return []}
        guard let list = cityM.readings else {return []}
        
        return getList(list: list)
    }
    
    public func provinceIdx(pro : String) -> Int {
        guard let cityM = cityModel else {return 0}
        
        guard let list = cityM.readings else {return 0}
        
        var index = 0
        for (idx,value) in list.enumerated() {
            if value.pardons == pro {
                index = idx
                break
            }
        }
        return index
    }
    
    public func getCityList(index : Int) -> [String] {
        guard let cityM = cityModel else {return []}
        guard let list = cityM.readings else {return []}
        
        if (index >= list.count) {return []}
        
        guard let cityList = list[index].readings else {return []}
        
        return getList(list: cityList)
    }
    
    public func cityIdx(provinceIdx : Int,cityName : String) -> Int{
        let cityList = getCityList(index: provinceIdx)
        
        var cityIdx = 0
        for (idx,value) in cityList.enumerated() {
            if (value == cityName) {
                cityIdx = idx
                break
            }
        }
        return cityIdx
    }
    
    public func getDistrictList(provinceIdx : Int,cityIndex : Int) -> [String] {
        guard let cityM = cityModel else {return []}
        guard let list = cityM.readings else {return []}
        
        if (provinceIdx >= list.count) {return []}
        
        guard let cityList = list[provinceIdx].readings else {return []}
        
        if (cityIndex >= cityList.count) {return []}
        
        guard let districtList = cityList[cityIndex].readings else {return []}
        
        return getList(list: districtList)
    }
    
    public func districtIdx(provinceIdx : Int,cityIndex : Int, districtName : String) -> Int {
        let districtList = getDistrictList(provinceIdx: provinceIdx, cityIndex: cityIndex)
        
        var districtIdx = 0
        for (idx,value) in districtList.enumerated() {
            if (value == districtName) {
                districtIdx = idx
                break
            }
        }
        return districtIdx
    }
}

extension XQCityManager {
    private func jsonString<T: HandyJSON>(from model: T) -> String? {
        if let jsonString = model.toJSONString() {
            return jsonString
        }
        return nil
    }
    
    private func getList(list : [XQPCitySubModel]) -> [String] {
        var provinceList : [String] = []
        for m in list {
            if let str = m.pardons {
                provinceList.append(str)
            }
        }
        return provinceList
    }
}
